package com.carlinfo.userscenter.servlet.back;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.carlinfo.userscenter.common.util.DateUtil;
import com.carlinfo.userscenter.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.pojo.AAdmins;
import com.carlinfo.userscenter.users.service.IUsersService;
import com.carlinfo.userscenter.users.service.impl.UsersServiceImpl;

public class AdminsBackServlet extends HttpServlet
{
	private IUsersService usersService = new UsersServiceImpl();

	private DateUtil dateUtil = new DateUtil();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("==AdminsBackServlet=service===");

		String method = request.getParameter("method");
		if ("main".equalsIgnoreCase(method))
		{
			main(request, response);
		} else if ("welcome".equalsIgnoreCase(method))
		{
			welcome(request, response);
		} else if ("logout".equalsIgnoreCase(method))
		{
			logout(request, response);
		} else if ("adminsList".equalsIgnoreCase(method))
		{
			adminsList(request, response);
		}
	}

	private void adminsList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		try
		{

			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}

		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");

		if (keyword == null)
		{
			keyword = "";
		}

		if (status == null)
		{
			status = "";
		}

		if (st == null)
		{
			st = "";
		}

		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;

		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = dateUtil.strToDateTime(st);
			edDate = dateUtil.strToDateTime(ed);
		}

		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("stTime", stDate);
		condMap.put("edTime", edDate);
		condMap.put("orderby", "idasc");
		condMap.put("extend", "ture");
		System.out.println("==查询条件==" + condMap);
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);

		String path = "/WEB-INF/jsp/back/admins/adminsList.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		HttpSession session = request.getSession();
		session.removeAttribute("adminsSess");
		String info = "退出成功,请放心";
		session.setAttribute("info", info);
		response.sendRedirect(request.getContextPath() + "/NoLoginBackServlet?method=login");
	}

	private void welcome(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/back/admins/welcome.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}

	private void main(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String path = "/WEB-INF/jsp/back/admins/main.jsp";
		request.getRequestDispatcher(path).forward(request, response);
	}
}
